<?php 

header("Content-type: image/jpeg");  


/**
* 随机生成验证码
*/
class Code
{	
	//属性
	public $witdh;
	public $height;			//画布大小
	public $res;			//画布
	public $num1;			//设定一个循环小于值
	public $num2;			//干扰线条值
	public $marin;			//字体间距设置
	
//	构造函数，初始化	
public function __construct($witdh=500,$height=200,$num1=5,$num3=1500,$num2=50){
	$this->witdh=$witdh;
	$this->height=$height;
	$this->num1=$num1;
	$this->num3=$num3;
	$this->num2=$num2;
		
}

	public function show()
	{
		$this->canvas();	//画布
		$this->fonts();		//字体
		$this->lines();		//线
		$this->point();		//点
		$this->out();		//输出
		//封装函数
	}


public function canvas(){
	$this->res=imageCreateTrueColor($this->witdh,$this->height);
	$color=imagecolorallocate($this->res, 234, 234, 234);
	imageFill($this->res,0,0,$color);
		//生成画布，设置颜色，填充颜色
		
		
}

//生成随机字母数字
public function fonts(){
	
	for ($i=0; $i <$this->num1 ; $i++) {

	$str = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
	$code = $str[mt_rand(0,strlen($str)-1)];

	$this->marin = $i*80;
	$this->marin+=70;

	$color=imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//随机颜色
	imagettftext($this->res,$this->witdh/$this->num1 ,mt_rand(-10,10) ,$this->marin, mt_rand($this->height/2,$this->height),$color, './TENOCLOCK-Regular.ttf', $code);
		//字母个数宽高与$witdh，$height相关联，改变画布大小，字体大小随之改变
		//保证字母位置在画布中央区域

		}
}
		
//生成干扰线条	
public function lines(){

	for ($j=0; $j <$this->num2 ; $j++) { 
	$color=imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
	imageline($this->res, mt_rand(0,$this->witdh), mt_rand(0,$this->height), rand(0,$this->witdh), rand(0,$this->height), $color);//线条
	}		
}

//生成干扰点
public function point(){

	for ($k=0; $k <$this->num3 ; $k++) { 
	$color=imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
	imagesetpixel ($this->res, mt_rand(0,$this->witdh), mt_rand(0,$this->height),$color);
	}

		
}

public function out(){
	imagejpeg($this->res);//输出画布
}


}



 ?>